package it.uiip.crm.persistenza;

import it.uiip.crm.model.Post;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

public class DAOPost {
    
	public static Post findByIDPost(int idPost) throws DAOException {
        Post post = null;
        try {
            Session session = PersistenceManager.getSession();

            Query query = session.createQuery("from Post where id = ?");

            query.setInteger(0, idPost);
            List<Post> risultato = query.list();
            if (risultato.size() != 0) {
            	post = risultato.get(0);
            }
        } catch (HibernateException e) {
            throw new DAOException(e);
        }
        return post;
    }

	public static Post findByTextPost(String testoPost) throws DAOException {
        Post post = null;
        try {
            Session session = PersistenceManager.getSession();

            Query query = session.createQuery("from Post where testo = ?");

            query.setString(0, testoPost);
            List<Post> risultato = query.list();
            if (risultato.size() != 0) {
            	post = risultato.get(0);
            }
        } catch (HibernateException e) {
            throw new DAOException(e);
        }
        return post;
    }
	
	public static List<Post> findByIDUtente(int idUtente) throws DAOException {
        
        try {
            Session session = PersistenceManager.getSession();

            Query query = session.createQuery("from Post where utente = ?");

            query.setInteger(0, idUtente);
            return query.list();
            
        } catch (HibernateException e) {
            throw new DAOException(e);
        }

    }
    
    public static List<Post> find() throws DAOException {
        
        try {
            Session session = PersistenceManager.getSession();

            Query query = session.createQuery("from Post");

            List<Post> list = query.list();
            return list;
            
        } catch (HibernateException e) {
            throw new DAOException(e);
        }
    }

    public static void makeRefresh(Post post) throws DAOException {
        PersistenceManager.beginTransaction();
        try {
            PersistenceManager.refreshObject(post);
        } catch (HibernateException e) {
            PersistenceManager.rollbackTransaction();
            throw new DAOException(e);
        }
    }
    
    public static void makePersistent(Post post) throws DAOException {
        PersistenceManager.beginTransaction();
        try {
            PersistenceManager.getSession().saveOrUpdate(post);
            PersistenceManager.commitTransaction();
        } catch (HibernateException e) {
            PersistenceManager.rollbackTransaction();
            throw new DAOException(e);
        }
    }
    
    public static void makeTransient(Post post) throws DAOException {
        PersistenceManager.beginTransaction();
        try {
            PersistenceManager.getSession().delete(post);
            PersistenceManager.commitTransaction();
        } catch (HibernateException e) {
            PersistenceManager.rollbackTransaction();
            throw new DAOException(e);
        }
    }
}
